// pages/operation/clueManage/tryListen/index.js
import { StudentModel } from "../../../../api/api-work/student.js"
import { LessonModel } from "../../../../api/api-work/lesson.js"

import Toast from '@vant/weapp/toast/toast';
import moment from "moment"
const studentModel = new StudentModel()
const lessonModel = new LessonModel()


Page({

  /**
   * 页面的初始数据
   */
  data: {
    query: {},
    courseId: "",
    courseName: "请选择课程",
    classStartTime: "",
    classEndTime: "",
    pageNum: 1,
    total: 0,
    tableData: [],
    tableDataObj: {},
    loadStatus: "",
    triggered: false,
    showLesson: false,
    lessonOptions: [], // 课程列表
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const param = JSON.parse(options.param);
    this.setData({
      query: param,
      classStartTime: moment().format('YYYY-MM-DD'),
      classEndTime: moment().add(6, 'days').format('YYYY-MM-DD'),
    })
    console.log("试听：", this.data.query)
    // this.getOrderFilterList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      triggered: false,
      pageNum: 1,
      tableDataObj: {},
      tableData: []
    })
    this.getOrderFilterList()
  },

  // 试听列表
  getOrderFilterList() {
    studentModel.getOrderFilterListApi({ pageNum: this.data.pageNum, pageSize: 15, classStartTime: this.data.classStartTime, classEndTime: this.data.classEndTime, stuId: this.data.query.id, courseId: this.data.courseId }).then(res => {
      let rows = this.data.tableData;
      rows.push(...res.rows);
      this.setData({
        tableData: rows,
        total: res.total
      })
      let obj = {};
      this.data.tableData.forEach(item => {
        if (obj.hasOwnProperty(item.classDate)) {
          obj[item.classDate].push(item)
        }
        else {
          obj[item.classDate] = [item]
        }
      })
      this.setData({
        tableDataObj: obj
      })
      if (this.data.total == 0) {
        this.setData({
          loadStatus: ""
        })
      }
      else if (this.data.tableData.length < this.data.total) {
        this.setData({
          loadStatus: "loadmore"
        })
      }
      else {
        this.setData({
          loadStatus: "nomore"
        })
      }
    })
  },

  getMoreData() {
    if (this.data.loadStatus === "nomore") {
      return;
    }
    this.setData({
      pageNum: this.data.pageNum + 1,
      loadStatus: "loading"
    })
    this.getOrderFilterList()
  },

  refreshMoreData() {
    this.setData({
      triggered: false,
      pageNum: 1,
      tableDataObj: {},
      tableData: []
    })
    this.getOrderFilterList()
  },

  // 增加日期
  addDate() {
    this.setData({
      classStartTime: moment(this.data.classEndTime).add(1, 'days').format('YYYY-MM-DD'),
      classEndTime: moment(this.data.classEndTime).add(7, 'days').format('YYYY-MM-DD'),
      triggered: false,
      pageNum: 1,
      tableDataObj: {},
      tableData: []
    })
    this.getOrderFilterList()
  },
  // 缩减日期
  minusDate() {
    const diff = moment().diff(moment(this.data.classStartTime), 'days')
    if (diff >= 0) {
      Toast('只能预约今天以后的班级');
      return;
    }

    this.setData({
      classEndTime: moment(this.data.classStartTime).add(-1, 'days').format('YYYY-MM-DD'),
      classStartTime: moment(this.data.classStartTime).add(-7, 'days').format('YYYY-MM-DD'),
      triggered: false,
      pageNum: 1,
      tableDataObj: {},
      tableData: []
    })
    this.getOrderFilterList()
  },

  // 选择课程
  chooseLesson() {
    lessonModel.getLessonApi({ open: 1, pageNum: 1, pageSize: 999 }).then(res => {
      this.setData({
        showLesson: true,
        lessonOptions: res.rows
      })
    })
  },
  // 确定选中课程
  chooseSelect(e) {
    const rows = e.detail;
    if (rows.length == 0) {
      // 所有
      this.setData({
        courseId: "",
        courseName: "请选择课程",
        pageNum: 1,
        tableDataObj: {},
        tableData: [],
        showLesson: false
      })
    }
    else {
      this.setData({
        courseId: rows[0].id,
        courseName: rows[0].name,
        pageNum: 1,
        tableDataObj: {},
        tableData: [],
        showLesson: false
      })
    }
    this.getOrderFilterList()
  },

  // 预约
  mavigateToListenDetail(e) {

    const param = JSON.stringify({ ...e.currentTarget.dataset.query, studId: this.data.query.id })
    wx.navigateTo({
      url: `/pages/operation/clueManage/tryListenDetail/index?param=${param}`,
    })
  },

  // 开班试听
  navigateToOpenClass() {
    const param = JSON.stringify(this.data.query);
    wx.navigateTo({
      url: `/pages/operation/clueManage/openClassListen/index?param=${param}`,
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})